﻿using System;
using System.ServiceModel;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Xyz.DataObjects;
using Xyz.DataObjects.Interfaces;
using Xyz.Service.DataTransferObjects;
using Xyz.BusinessObjects;
using Xyz.Service.ServiceContracts;

namespace Xyz.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CustomerService : ICustomerService
    {
        public static readonly ICustomerDao CustomerDao = DataAccess.CustomerDao;
        public static readonly IMemberDao MemberDao = DataAccess.MemberDao;
        public static readonly IAgentDao AgentDao = DataAccess.AgentDao;
        private static readonly IRoleDao RoleDao = DataAccess.RoleDao;

        public string GetCustomerTypeDescription(int customerType)
        {
            return CustomerDao.GetCustomerTypeDescription(customerType);
        }

        public List<CustomerDTO> GetCustomersLike(string firstName, string lastName)
        {
            return Mapper.ToDto(CustomerDao.GetCustomersLike(firstName, lastName));
        }

        public void CreateCustomer(CustomerDTO customer)
        {
            Customer c = Mapper.FromDto(customer);

            if (c.Validate())
            {
                if (CustomerDao.IsUserNameExists(c.UserName))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("User Name ");
                    sb.Append(c.UserName);
                    sb.Append(" is taken by another user. Please choose other User Name.");
                    throw new Exception(sb.ToString());
                }
                else
                {
                    CustomerDao.CreateCustomer(c);
                }
            }
        }

        public void UpdateCustomer(CustomerDTO customer)
        {
            Customer c = Mapper.FromDto(customer);

            if (c.Validate())
            {
                CustomerDao.UpdateCustomer(c);
            }
        }

        public int GetMemberPoints(int customerId)
        {
            return MemberDao.GetMemberPoints(customerId);
        }

        public void AddMemberPoints(int customerId, int points)
        {
            MemberDao.AddMemberPoints(customerId, points);
        }

        public CustomerDTO LoginCustomer(string userName, string password)
        {
            return Mapper.ToDto(CustomerDao.LoginCustomer(userName, password));
        }

        public string GetCustomerRole(string userName)
        {
            return RoleDao.GetCustomerRole(userName);
        }
    }
}
